const app = getApp();
var imgBaseUrl = app.globalData.imgBaseUrl;
var imgPreUrl = app.globalData.imgPreUrl;
Page({
  data: {
    userid:'',
    imgBaseUrl: imgBaseUrl,
    imgPreUrl: imgPreUrl,
    isKong:false,
    kongBgImg: imgBaseUrl +"/address_bg.png",
    addressList:[],
    sel:0
  },
  onLoad: function (options) {
    var that=this; 
    if(options.flag){
      this.setData({
        flag:options.flag
      })
    } 
  },
  bindSelectAdd:function(e){
    var addressList=this.data.addressList;
    var index=e.currentTarget.dataset.index;
    var seladdress={};

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    console.log(addressList);
    console.log(addressList[index]);
    prevPage.setData({
      address: addressList[index]
    })
    wx.navigateBack({
    })
  },
  onShow: function () {
    var that=this;
    app.func.postReq("/text/index.php/home/address/addresslists",{},function(res){
      if (res.address!=null) {
        var isKong = false;
      } else {
        var isKong = true;
      }
      that.setData({
        addressList: res.address,
        isKong: isKong
      });    
    });
  },
  bindStateEdit:function(e){
    var that=this;
    var addressList = that.data.addressList;
    var index=e.currentTarget.dataset.index;
    var id = addressList[index].id,
        len = addressList.length,
        state = addressList[index].state;
    if(parseInt(state)==2){
      app.func.postReq("/text/index.php/home/address/defaultaddress", { id: id, state: 1 }, function (res) {
        if (parseInt(res) == 1) {
          wx.showToast({
            title: '设置成功',
            icon: "success"
          })
          for (let i = 0; i < len; i++) {
            addressList[i].state = 2;
          }
          addressList[index].state = 1;
          that.setData({
            addressList: addressList
          })
        }
      });
    }
  },
  bindDelAddress:function(e){
    var that=this;
    wx.showModal({
      content: '是否删除改地址？',
      success: function (res) {
        if (res.confirm) {
          var addressList = that.data.addressList;
          var id = e.currentTarget.dataset.id,
            index = e.currentTarget.dataset.index;
          if (parseInt(addressList[index].state) == 1) {
            wx.showToast({
              title: '默认地址不能删除~',
              icon: 'none'
            })
          } else {
            app.func.postReq("/text/index.php/home/address/deladdress", { id: id }, function (res) {
              if (parseInt(res) == 1) {
                wx.showToast({
                  title: '删除成功',
                  icon: "success"
                })
                addressList.splice(index, 1);
                if (addressList.length > 0) {
                  var isKong = false;
                } else {
                  var isKong = true;
                }
                that.setData({
                  addressList: addressList,
                  isKong: isKong
                })
              }
            })
          }
        } else if (res.cancel) {
          return;
        }
      }
    })
    
  },
})